﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class ISYC_303_12O_F34c
    {
        public static void ISYC_303_12O_F34c_H(Dictionary<string, Object> data) //整体O型桶式立铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double OAL = 65.55;
            double LPR = 32.77;
            double APMX = 12.85;
            double DC = 9.29;
            double LH = 25.16;
            double DCONMS = 9.29;
            double scale = 65.55 / OAL;

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, 0 * pi / 180, OAL);
            Vector2 v3 = fun.Polar(v1, 90 * pi / 180, DCONMS * 0.5 * 0.8);
            Vector2 v4 = fun.Inters(v3, fun.Polar(v3, 45 * pi / 180, OAL), fun.Polar(v1, 90 * pi / 180, DCONMS / 2.0), new Vector2(v1.X + OAL, v1.Y + DCONMS / 2.0));
            Vector2 v5 = fun.Polar(v4, 0 * pi / 180, (44.05 / 65.55) * OAL);
            Vector2 v6 = new Vector2(v5.X, -v5.Y);
            Vector2 v7 = new Vector2(v4.X, -v4.Y);
            Vector2 v8 = new Vector2(v3.X, -v3.Y);
            Vector2 v9 = fun.Polar(v5, 180 * pi / 180, (4.32 / 12.85) * APMX);
            Vector2 v10 = fun.Polar(v5, -2 * pi / 180, (3.94 / 12.85) * APMX);
            Vector2 v11= fun.Polar(v10, -6 * pi / 180, (2.64 / 12.85) * APMX);
            Vector2 v12 = fun.Polar(v10, -155 * pi / 180, (5.67 / 12.85) * APMX);
            Vector2 v13= fun.Polar(v12, -95 * pi / 180, (5.67 / 9.29) * DC);
            Vector2 v14 = fun.Polar(v11, -11 * pi / 180, (5.68 / 12.85) * APMX);
            Vector2 v15 = fun.Polar(v14, -15 * pi / 180, (0.84 / 12.85) * APMX);
            Vector2 v16 = fun.Polar(v15, -16 * pi / 180, (1.7 / 12.85) * APMX);
            Vector2 v17 = new Vector2(v2.X - LH, v6.Y);
            Vector2 v18= fun.Polar(v13, 7 * pi / 180, (0.92 / 12.85) * APMX);
            Vector2 v19= fun.Polar(v13, 7 * pi / 180, (1.85 / 12.85) * APMX);
            Vector2 v20 = fun.Polar(v18, -90 * pi / 180, (0.67 / 9.29) * DC);
            Vector2 v21 = fun.Polar(v19, -90 * pi / 180, (0.51 / 9.29) * DC);
            Vector2 v22 = fun.Polar(v16, -19 * pi / 180, (3.51 / 12.85) * APMX);
            Vector2 v23 = fun.Polar(v6, 4 * pi / 180, (7.66 / 12.85) * APMX);
            Vector2 v24= fun.Polar(v22, -22 * pi / 180, (1.35 / 12.85) * APMX);
            Vector2 v25 = fun.Polar(v23, 11 * pi / 180, (3.18 / 12.85) * APMX);
            Vector2 v26 = fun.Polar(v9, -26 * pi / 180, (3.34 / 9.29) * DC);
            Vector2 v27 = new Vector2(v2.X - LPR, v2.Y);
            Vector2 v28 = fun.Inters(v14, fun.Polar(v14, -157 * pi / 180, OAL), v1, v2);
            Vector2 v29 = fun.Inters(v15, fun.Polar(v15, -157 * pi / 180, OAL), v1, v2);
            Vector2 v30 = fun.Inters(v16, fun.Polar(v15, -159 * pi / 180, OAL), v1, v2);
            Vector2 v31 = fun.Inters(v22, fun.Polar(v15, -155 * pi / 180, OAL), v1, v2);
            Vector2 v32 = fun.Inters(v24, fun.Polar(v15, -161 * pi / 180, OAL), v1, v2);
            Vector2 v33 = fun.Polar(v7, -137 * pi / 180, (34.44 / 9.29) * DC);
            Vector2 v34 = fun.Polar(v27, 0 * pi / 180, (4.23 / 32.77) * LPR);
            Vector2 v35 = fun.Polar(v2, 0 * pi / 180, (4.23 / 32.77) * LPR);

            //第二部分
            //虚线
            Vector2 v36 = fun.Polar(v7, -84 * pi / 180, (35.6 / 9.29) * DC);
            Vector2 v37 = fun.Polar(v36, 0 * pi / 180, (65.13 / 65.55) * OAL);
            Vector2 v38 = fun.Polar(v37, -90 * pi / 180, (32.5 / 9.29) * DC);
            Vector2 v39 = fun.Polar(v36, -90 * pi / 180, (32.5 / 9.29) * DC);

            //轮廓
            Vector2 v40 = fun.Polar(v36, -62 * pi / 180, (20.62 / 9.29) * DC);
            Vector2 v41 = fun.Polar(v40, 0 * pi / 180, (15.75 / 65.55) * OAL * 1.2);
            Vector2 v42 = fun.Polar(v41, -11 * pi / 180, (23.85 / 65.55) * OAL * 1.2);
            Vector2 v43 = fun.Polar(v42, -169 * pi / 180, (23.85 / 65.55) * OAL * 1.2);
            Vector2 v44 = fun.Polar(v40, -90 * pi / 180, (9.43 / 9.29) * DC * 1.2);

            //字
            Vector2 v45 = fun.Polar(v36, 4 * pi / 180, (29.08 / 65.55) * OAL);

            //标注
            Vector2 v46 = fun.Polar(v41, -8 * pi / 180, (17.62 / 65.55) * OAL);
            Vector2 v47 = fun.Polar(v42, -158 * pi / 180, (1.18 / 12.85) * APMX);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //中心线
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.Center;
            line1.LinetypeScale = 5;
            dxf.AddEntity(line1);

            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));//0
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));//1
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));//2
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));//3
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));//4
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));//5
            polyline1.Vertexes.Add(new LwPolylineVertex(v8));//6
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));//7
            polyline1.Vertexes[3].Bulge = Math.Tan(-25 * pi / 180 / 4);
            polyline1.Vertexes[4].Bulge = Math.Tan(-25 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline1, 0.3, contour);

            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v9));
            polyline2.Vertexes.Add(new LwPolylineVertex(v26));//0
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));//1
            polyline2.Vertexes[0].Bulge = Math.Tan(39 * pi / 180 / 4);
            polyline2.Vertexes[1].Bulge = Math.Tan(-32 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v11));
            polyline3.Vertexes.Add(new LwPolylineVertex(v12));//0
            polyline3.Vertexes[0].Bulge = Math.Tan(35 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v11));
            polyline4.Vertexes.Add(new LwPolylineVertex(v12));//0
            polyline4.Vertexes.Add(new LwPolylineVertex(v13));//1
            polyline4.Vertexes.Add(new LwPolylineVertex(v28));//2
            polyline4.Vertexes.Add(new LwPolylineVertex(v14));//3
            polyline4.Vertexes[0].Bulge = Math.Tan(39 * pi / 180 / 4);
            polyline4.Vertexes[1].Bulge = Math.Tan(134 * pi / 180 / 4);
            polyline4.Vertexes[2].Bulge = Math.Tan(11 * pi / 180 / 4);
            polyline4.Vertexes[3].Bulge = Math.Tan(-16 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline4, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v13));
            polyline5.Vertexes.Add(new LwPolylineVertex(v18));//0
            polyline5.Vertexes.Add(new LwPolylineVertex(v19));//1
            polyline5.Vertexes.Add(new LwPolylineVertex(v29));//2
            polyline5.Vertexes.Add(new LwPolylineVertex(v15));//3
            polyline5.Vertexes[2].Bulge = Math.Tan(8 * pi / 180 / 4);
            polyline5.Vertexes[3].Bulge = Math.Tan(-11 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v17));
            polyline6.Vertexes.Add(new LwPolylineVertex(v30));//0
            polyline6.Vertexes.Add(new LwPolylineVertex(v16));//1
            polyline6.Vertexes[0].Bulge = Math.Tan(22 * pi / 180 / 4);
            polyline6.Vertexes[1].Bulge = Math.Tan(-14 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v23));
            polyline7.Vertexes.Add(new LwPolylineVertex(v31));//0
            polyline7.Vertexes.Add(new LwPolylineVertex(v22));//1
            polyline7.Vertexes[0].Bulge = Math.Tan(15 * pi / 180 / 4);
            polyline7.Vertexes[1].Bulge = Math.Tan(-16 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v25));
            polyline8.Vertexes.Add(new LwPolylineVertex(v32));//0
            polyline8.Vertexes.Add(new LwPolylineVertex(v24));//1
            polyline8.Vertexes[0].Bulge = Math.Tan(13 * pi / 180 / 4);
            polyline8.Vertexes[1].Bulge = Math.Tan(-16 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            LwPolyline polyline9 = new LwPolyline();
            polyline9.Vertexes.Add(new LwPolylineVertex(v4));
            polyline9.Vertexes.Add(new LwPolylineVertex(v7));
            fun.PolyFill(dxf, polyline9, 0.3, contour);

            //第二部分
            //轮廓
            LwPolyline polyline13 = new LwPolyline();
            polyline13.Vertexes.Add(new LwPolylineVertex(v40));
            polyline13.Vertexes.Add(new LwPolylineVertex(v41));//0
            polyline13.Vertexes.Add(new LwPolylineVertex(v42));//1
            polyline13.Vertexes.Add(new LwPolylineVertex(v43));//2
            polyline13.Vertexes.Add(new LwPolylineVertex(v44));//3
            polyline13.Vertexes.Add(new LwPolylineVertex(v40));//4
            polyline13.Vertexes[1].Bulge = Math.Tan(-23 * pi / 180 / 4);
            polyline13.Vertexes[2].Bulge = Math.Tan(-23 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline13, 0.3, contour);

            //虚线
            LwPolyline polyline14 = new LwPolyline();
            polyline14.Vertexes.Add(new LwPolylineVertex(v36));
            polyline14.Vertexes.Add(new LwPolylineVertex(v37));
            polyline14.Vertexes.Add(new LwPolylineVertex(v38));
            polyline14.Vertexes.Add(new LwPolylineVertex(v39));
            polyline14.Vertexes.Add(new LwPolylineVertex(v36));
            polyline14.Linetype = Linetype.Dashed;
            polyline14.LinetypeScale = 5;
            polyline14.Layer = refin;
            dxf.AddEntity(polyline14);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v41));//0
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));//1
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v43));//2
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v44));//3
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));//4
            hatchpoly1.Vertexes[1].Bulge = Math.Tan(-23 * pi / 180 / 4);
            hatchpoly1.Vertexes[2].Bulge = Math.Tan(-23 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            //标注---------------------------------------------------------------------------
            double height = 2 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            fun.Axis(dxf, v1, 90, 180, "", "", "PCS", 0.3 * scale, height, 0);
            fun.Axis(dxf, v27, 90, 180, "", "", "MCS", 0.3 * scale, height, 0);
            fun.Axis(dxf, v2, 90, 180, "", "", "CIP", 0.3 * scale, height, 0);
            fun.Axis(dxf, v33, 90, 180, "Z", "Y", "X", 0.3 * scale, height, 0);

            //直线标注
            //DCONMS
            fun.dimrot1(dxf, v7, v4, 90, APMX * 0.5, 0.1, height, arrowsize, exline, "DCONMS", "", "");

            //OAL
            fun.dimrot1(dxf, v3, v2, 0, DC * 2, 0.1, height, arrowsize, exline, "OAL,LF", "", "");

            //LPR
            fun.dimrot1(dxf, v27, v2, 0, DC * 1.5, 0.1, height, arrowsize, exline, "LPR,LTA", "", "");

            //APMX
            fun.dimrot1(dxf, v11, v2, 0, DC * 0.8, 0.1, height, arrowsize, exline, "APMX", "", "");

            //DC
            fun.dimrot1(dxf, v5, v6, -90, APMX * 3, 0.1, height, arrowsize, exline, "DC", "", "");

            //LH
            fun.dimrot1(dxf, v17, v2, 180, DC * 1.5, 0.1, height, arrowsize, exline, "LH", "", "");

            //箭头
            //CCMS
            fun.jt1(dxf, fun.Polar(v4, 0 * pi / 180, (OAL - LPR) / 2.0), 135, 5, 1, "CCMS", height);

            //PRDRAD
            fun.jt1(dxf, v46, 45, 5, 1, "PRFRAD", height);

            //RE
            fun.jt1(dxf, v47, -35, 5, 1, "RE", height);

            //A
            fun.jt1(dxf, v34, 90, 5, 1, "A", height);
            fun.jt1(dxf, v35, 90, 5, 1, "A", height);

            //FDP
            fun.jt1(dxf, fun.Polar(v2, 50 * pi / 180, (27.88 / 16.97) * DC), -100, 5, 1, "FDP", height);

            //字符
            fun.text(dxf, fun.Polar(v36, 4 * pi / 180, (29.08 / 65.55) * OAL), height, "A-A");

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
